package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.util.FastJsonUtils;
import com.common.vo.MessageLogVo;
import com.data.core.dao.MessageLogMapper;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.data.core.entry.MessageLog;
import com.data.core.service.MessageLogService;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: MessageLogServiceImpl
 * @packageName: MessageLogServiceImpl
 * @description: 发送消息相关接口实现
 * @data: 2021-1-27
 **/
@Service
public class MessageLogServiceImpl extends ServiceImpl<MessageLogMapper, MessageLog> implements MessageLogService {

    @Resource
    private  MessageLogMapper messageLogMapper;

    /**
     *  保存发送消息
     * @param messageLogVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    public int save(MessageLogVo messageLogVo) throws Exception {
        if (Objects.isNull(messageLogVo)){
            throw new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try{
            MessageLog messageLog = new MessageLog();
            BeanUtils.copyProperties(messageLogVo,messageLog);
            return  messageLogMapper.insert(messageLog);
        }catch (Exception e){
            throw new BaseException(BaseEnum.SAVE_MESSAGE_IS_ERROR);
        }
    }

    /**
     * 更新发送的消息
     * @param messageLogVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    public int update(MessageLogVo messageLogVo) throws Exception {
        if (Objects.isNull(messageLogVo)){
            throw new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try{
            MessageLog messageLog = new MessageLog();
            BeanUtils.copyProperties(messageLogVo,messageLog);
            return messageLogMapper.updateById(messageLog);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_MESSAGE_IS_ERROR);
        }
    }

    /**
     * 查询发送失败消息接口实现
     * @param messageLogVo
     * @return
     * @throws Exception
     */
    @Override
    public String findFailureMes(MessageLogVo messageLogVo) throws Exception {
        if (Objects.isNull(messageLogVo)){
            throw new BaseException(BaseEnum.DATA_IS_NULL);
        }
        try{
            QueryWrapper<MessageLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mes_status",messageLogVo.getMesStatus());
            List<MessageLog>  messageLogList =  messageLogMapper.selectList(queryWrapper);
            List<MessageLogVo> messageLogVos = new ArrayList<>();
            if (CollectionUtils.isEmpty(messageLogList)){
                return  null;
            }
            messageLogList.forEach(item->{
                MessageLogVo messageLogVo1 =new MessageLogVo();
                BeanUtils.copyProperties(item,messageLogVo1);
                messageLogVos.add(messageLogVo1);
            });
            return FastJsonUtils.toJSONString(messageLogVos);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_MESSAGE_IS_ERROR);
        }
    }
}
